// import buble from '@rollup/plugin-buble'
import replace from '@rollup/plugin-replace'
import {
  nodeResolve
} from '@rollup/plugin-node-resolve'
import commonjs from '@rollup/plugin-commonjs'
import babel from 'rollup-plugin-babel';
import {
  terser
} from 'rollup-plugin-terser'
import async from 'rollup-plugin-async'
import pkg from './package.json'
import json from '@rollup/plugin-json'

const banner = `/*!
 * tb-request v${pkg.version}
 * (c) ${new Date().getFullYear()} Thunbu
 * @license ISC
 */`

export function createEntries(configs) {
  return configs.map((c) => createEntry(c))
}

function createEntry(config) {
  const c = {
    input: config.input,
    plugins: [json(), async (), babel({
      exclude: '**/node_modules/**',
      runtimeHelpers: true
    })],
    output: {
      banner,
      file: config.file,
      format: config.format
    },
    onwarn: (msg, warn) => {
      if (!/Circular/.test(msg)) {
        warn(msg)
      }
    }
  }

  if (config.format === 'umd') {
    c.output.name = c.output.name || 'tbReqest'
  }

  c.plugins.push(replace({
    __VERSION__: pkg.version,
    __DEV__: config.format !== 'umd' && !config.browser ?
      `(process.env.NODE_ENV !== 'production')` : config.env !== 'production'
  }))

  if (config.transpile !== false) {
    c.plugins.push(babel({
      exclude: '**/node_modules/**',
      runtimeHelpers: true
    }))
  }

  c.plugins.push(nodeResolve({
    browser: true
  }))
  c.plugins.push(commonjs({
    include: "node_modules/**"
  }))
  c.plugins.push(terser({
    compress: {
      drop_console: true
    }
  }))
  if (config.minify) {
    c.plugins.push(terser({
      module: config.format === 'es'
    }))
  }

  return c
}